package com.global.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class AutoRegistro
 */
@WebServlet("/AutoRegistro")
public class AutoRegistro extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DataSource pool;	
	
	@Override
	public void init(ServletConfig config) throws ServletException {
	super.init(config);
    InitialContext ctx=null;
	try {
		ctx = new InitialContext();
	} catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    // Lookup the DataSource, which will be backed by a pool
    // that the application server provides.
    try {
		pool = (DataSource)ctx.lookup("java:comp/env/jdbc/mysql_globalservice");
	} catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    if (pool == null)
    throw new ServletException("Unknown DataSource 'jdbc/mysql_globalservice'");
    }
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AutoRegistro() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.println("<html><head><title>Query Insert</title></head><body>");
		
		Connection conn = null;
		Statement stmt = null;
		
		try {
			//String qty;
//			int id = Integer.parseInt(request.getParameter("cedula"));
			int id = 123;
//			boolean idAux = id != 0;			
//		    String nombre = request.getParameter("nombre");
		    String nombre = "gilberth";
//		    boolean nombreAux = nombre != null && ((nombre = nombre.trim()).length() > 0);	    
//		    String email = request.getParameter("email");
		    String email = "as@gmail.com";
//		    boolean emailAux = email != null && ((email = email.trim()).length() > 0);
		    
			if( id == 0/*!idAux || !nombreAux || !emailAux*/){
				out.println("<h3>Hay campos vacios que son requeridos!</h3>");
				//out.println("<p><a href='search'>Back to Select Menu</a></p>");
			} else {
				conn = pool.getConnection();
				stmt = conn.createStatement();
				String sqlStr = "SELECT idStudent FROM usuarios";
				ResultSet rset = stmt.executeQuery(sqlStr);
				
				while (rset.next()) { // list all the authors
					int p = rset.getInt("idStudent");
				    //String author = rset.getString("author");	    
				    out.println("<option value='" + p + "'>" + p + "</option>");
			    }
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			out.close();
			try {
				if (stmt != null) stmt.close();
				if (conn != null) conn.close();
			} catch (SQLException ex) {
				Logger.getLogger(AutoRegistro.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
